package reaction.europarl.queries;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;

import reaction.europarl.Definitions;
import reaction.europarl.queries.candidates.Candidate;


public class QueryEL {

	public String answer;		// the answer given by the system
	public String query_id;		// a query ID (a string uniquely identifying the query)
	public String query_string;	// a query string (a string referring to a named entity)
	public String support_doc;	// a query document (the name of a document containing the query string)
	public String etype;		// an entity type (PER for person, GPE for geo-political entity, ORG for organization, UKN for unknown. This collection should be 100% PER)
	public String KBID;			// KBID (a string indicating which entity in the knowledge base the query string refers to, or the string NIL if the entity is not in the KB, or the string UNKNOWN if no ground truth is available for the query)
	public String URL_answer;	// Wikipedia URL
	
	public Set<Candidate> candidates = new HashSet<Candidate>();
	public List<Candidate> candidatesRanked;
	
	public QueryEL(String query_id, String query_string, String support_doc, String etype, String kBID, String URL) {
		super();
		this.query_id = query_id;
		this.query_string = query_string;
		this.support_doc = support_doc;
		this.etype = etype;
		this.URL_answer = URL;
		KBID = kBID;
	}
	
	
	public QueryEL() {
		super();
	}
	
	public Candidate bestCandidate() {
		Candidate best = null;
		float best_score = 0;
		for (Candidate c : candidates) {
			if (c.lucene_score>best_score) {
				best = c;
				best_score = best.lucene_score;
			}
				
		}
		return best;
	}
	
	@Override
	public String toString() {
		
		StringBuilder result = new StringBuilder();
		String NEW_LINE = System.getProperty("line.separator");
		
	    result.append(" query id: " + query_id + NEW_LINE);
	    result.append(" query string: " + query_string + NEW_LINE);
	    result.append(" support doc: " + support_doc + NEW_LINE );
	    result.append(" entity type: " + etype + NEW_LINE);
	    result.append(" KBID: " + KBID + NEW_LINE);
	    result.append(" URL: " + URL_answer + NEW_LINE);
	    
	    return result.toString();
	}
	
	public void getSupportDocument() throws IOException {
        Term t = new Term("id", this.support_doc.split(".txt")[0]); 
        TermQuery query = new TermQuery(t);
        TopDocs docs = reaction.europarl.Definitions.europarlSearcher.search(query, 1);        
        ScoreDoc[] scoredocs = docs.scoreDocs;
        Document doc = Definitions.europarlSearcher.doc(scoredocs[0].doc);        
        this.support_doc = doc.get("text");
	}

	// Reciprocal rank
	public float reciprocalRank(){
		float reciprocalrank = 0;
		for (int i = 0; i < candidatesRanked.size(); i++) {
			String fullURL = "http://pt.wikipedia.org/wiki/"+candidatesRanked.get(i).URL;
			if (fullURL.equalsIgnoreCase(URL_answer)) {				
				reciprocalrank =  (float) 1 / (float) (i+1);
				break;
			}
		}
		return reciprocalrank;
		
	}
}
